import { goto } from '../goto';
import { TaWxapp } from './wxapp'
import { TaHttp, TaPost } from '../script/ajax';
import { TaCache, TaLoading, TaIsWeChat } from '../script/tools'

// 账号接口
export const TaDataUser = {

	// 加载用户数据
	my: {

		// 修改用户资料
		setting(data: any) {
			return TaPost('/plugin-account/api.auth.center/set', data)
		},

		// 加载用户推广记录
		spreads(data: any = {}): Promise<any> {
			return TaPost('/shop/api.auth.spread/get', data)
		},

		// 加载用户返利记录
		rebates(data: any = []): Promise<any> {
			return TaPost('/shop/api.auth.rebate/get', data)
		},

		// 加载用户提现记录
		transfers(data: any = []): Promise<any> {
			return TaPost('/shop/api.auth.transfer/get', data)
		},

		// 加载用户余额记录
		balances(data: any = {}): Promise<any> {
			return TaPost('/plugin-payment/api.auth.balance/get', data)
		},

		// 加载用户积分记录
		integrals(data: any = {}): Promise<any> {
			return TaPost('/plugin-payment/api.auth.integral/get', data)
		},

		// 加载用户海报
		posters(): Promise<any> {
			return TaPost('/shop/api.auth.spread/poster')
		}
	},

	// 读取本地用户
	local: {
		all(): any {
			return TaCache.get('auth.user') || null;
		},
		token(): string {
			return TaCache.get('auth.token') || '';
		},
		account(): any {
			return (TaCache.get('auth.user') || {}).account || {}
		},
		relation(): any {
			return (TaCache.get('auth.user') || {}).relation || {}
		},
		userinfo(): any {
			return (TaCache.get('auth.user') || {}).userinfo || { extra: {} }
		},
	},

	// 短信验证码
	verify: {

		// 发送短信验证码
		send(data: any): Promise<any> {
			data.uniqid = TaCache.get('verify.uniqid') || '';
			return TaPost('/plugin-account/api.login/send', data)
		},

		// 验证拼图验证结果
		check(value: Number): Promise<any> {
			return TaPost('/plugin-account/api.login/verify', {
				verify: value, uniqid: TaCache.get('verify.uniqid') || '',
			})
		},

		// 获取图形验证码
		image(): Promise<any> {
			return new Promise((resolve, reject) => {
				TaHttp.post('/plugin-account/api.login/image').then((ret: any) => {
					if (ret.code === 1) {
						TaCache.set('verify.uniqid', ret.data.uniqid)
						resolve(ret.data)
					} else {
						reject(ret)
					}
				});
			});
		},
	},

	// 初始化登录
	init() {

		// #ifdef H5
		// 微信服务号自动登录
		if (TaIsWeChat()) {
			let token = sessionStorage.getItem('wechat.token')
			if (token) TaCache.set('auth.token', token)
		}
		// #endif

		// #ifdef MP-WEIXIN
		// 微信小程序自动登录
		return TaWxapp.login();
		// #endif
	},

	// 通过手机号登录
	login(data: object = { type: String, phone: String, verify: String }): Promise<any> {
		TaLoading.show('登录中...')
		return new Promise((resolve, reject) => {
			TaHttp.post('/plugin-account/api.login/in', data).then((ret: any) => {
				if (ret.code === 1) {
					TaCache.set('auth.user', ret.data)
					resolve(ret.data), goto.back()
				} else {
					TaCache.del('auth.user'), reject(ret)
				}
			}).finally(() => TaLoading.hide());
		});
	},

	// 绑定完善资料
	bind(data: any): Promise<any> {
		TaLoading.show('绑定中...')
		return new Promise((resolve, reject) => {
			TaHttp.post('/plugin-account/api.auth.center/bind', data).then((ret: any) => {
				if (ret.code === 1) {
					TaCache.set('auth.user', ret.data)
					goto.back(), resolve(ret.data)
				} else {
					TaCache.del('auth.user'), reject(ret)
				}
			}).finally(() => TaLoading.hide());
		});
	},

	// 解绑主账号
	unbind(): Promise<any> {
		TaLoading.show('解绑中...')
		return new Promise((resolve, reject) => {
			TaHttp.post('/plugin-account/api.auth.center/unbind').then((ret: any) => {
				TaCache.del('auth.user')
				ret.code === 1 ? resolve(goto.home()) : reject(ret)
			}).finally(() => TaLoading.hide())
		})
	},

	// 设置用户代理
	from(unid: number) {
		TaCache.set('auth.from', unid)
	},

	// 退出登录并跳转
	logout(force: boolean = false): true {
		TaLoading.hide(), uni.clearStorageSync()
		if (force) {
			uni.redirectTo({ url: '/pages/user/login' })
		} else {
			uni.navigateTo({ url: '/pages/user/login' })
		}
		return true
	},

	// 判断是否登录
	isLogin(): boolean {
		return TaDataUser.local.all() !== null
	},

	// 刷新本地用户
	userinfo(): Promise<any> {
		return new Promise((resolve, reject) => {
			TaHttp.post('/shop/api.auth.center/get').then((ret: any) => {
				if (ret.code === 1) {
					let data: any = {
						account: ret.data.account,
						relation: ret.data.relation,
						userinfo: ret.data.account.user
					}
					delete data.account.user
					TaCache.set('auth.user', data), resolve(data)
				} else {
					reject(ret)
				}
			})
		})
	},

	// 获取用户等级
	levels(force: boolean = false): Promise<any> {
		return new Promise((resolve, reject) => {
			let items: any[] = TaCache.get('user.levels') || [];
			if (!force && items.length > 0) return resolve(items);
			TaHttp.post('/shop/api.auth.center/levels').then((ret: any) => {
				if (ret.code === 1) {
					resolve(ret.data)
					TaCache.set('user.levels', ret.data, 3600000)
				} else {
					reject(ret);
				}
			})
		});
	}
}

// 收货地址管理
export const TaDataAddress = {

	// 添加或更新地址
	set(data: any): Promise<any> {
		return TaPost('/plugin-payment/api.auth.address/set', data, '保存中...')
	},

	// 获取收货地址
	get(id?: string): Promise<any> {
		let data: any = {}
		if (id) data.id = id
		return TaPost('/plugin-payment/api.auth.address/get', data, '加载中...')
	},

	// 删除收货地址
	del(id: string): Promise<any> {
		let data: any = { id: id }
		return TaPost('/plugin-payment/api.auth.address/remove', data, '删除中...')
	},

	// 修改默认状态
	state(data: any): Promise<any> {
		return TaPost('/plugin-payment/api.auth.address/state', data)
	},
}